package com.powernode.controller;

import com.powernode.api.model.ProductInfo;
import com.powernode.api.pojo.PhoneBidInfo;
import com.powernode.api.service.BidInfoService;
import com.powernode.api.service.InvestService;
import com.powernode.api.service.ProductInfoService;
import com.powernode.common.Code;
import com.powernode.resp.Result;
import com.powernode.vo.BidInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ProductInfoController {

    @DubboReference(interfaceClass = ProductInfoService.class,version = "1.0.0")
    private ProductInfoService productInfoService;

    @DubboReference(interfaceClass = BidInfoService.class,version = "1.0.0")
    private BidInfoService bidInfoService;


    @DubboReference(interfaceClass = InvestService.class,version = "1.0.0")
    private InvestService investService;


    /*根据 产品id 获取产品的详情信息*/
    @GetMapping("/v1/product/detail")
    public Result queryByProductId(Integer id){
        /*object 储存 产品详情  list 储存 该产品的 投资记录*/
        Result result = Result.SUCCESS();
        /*获取产品详情*/
        ProductInfo productInfo = productInfoService.queryById(id);
        /*投资记录 多条     产品的 id     前 5  */
        List<PhoneBidInfo> phoneBidInfos = bidInfoService.queryBidByProductId(id,0,5);
        result.setObject(productInfo);
        result.setList(phoneBidInfos);
        return result;
    }

    /*投资*/
    @PostMapping("/v1/product/invest")
    public Result productInvest(@RequestBody BidInfo bidInfo){
        Result result = Result.FAILURE();
        /*判断参数*/
        if (bidInfo.getUid()!=null&&bidInfo.getUid()>0&&bidInfo.getBidMoney().intValue()%100==0){
            /*远程服务调用*/
            /*哪个人 投了 哪个产品 投了 多少钱*/
           /* try {
                     boolean investResult = investService.investProduct(bidInfo.getUid(),bidInfo.getProductId(),bidInfo.getBidMoney());
                result = Result.SUCCESS();
            }catch (Exception e) {
                   result.setCodeEnum(Code.PRODUCT_INVEST_ERROR);
            }*/
            boolean investResult = investService.investProduct(bidInfo.getUid(),bidInfo.getProductId(),bidInfo.getBidMoney());
            if (investResult){
                result = Result.SUCCESS();
            }else {
                result.setCodeEnum(Code.PRODUCT_INVEST_ERROR);
            }
        }else {
            result.setCodeEnum(Code.QUERY_PARAM_ERROR);
        }
        return result;
    }
}
